/* 
 * File:   cmdlineopts.cpp
 * Author: meng
 * 
 * Created on April 29, 2011, 10:55 AM
 */
#include <boost/program_options.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include "cmdlineopts.h"
#include <iostream>
#include <iterator>
#include <stdlib.h>

using namespace std;
using namespace opsrc;
using namespace boost;
using namespace boost::program_options;
using namespace boost::algorithm;


cmdlineopts::cmdlineopts() {
}

 

bool cmdlineopts::parse(int arg, char** argv) {
    namespace po = boost::program_options;

    po::options_description op("Allow options");
    op.add_options()
        ("help,h", "parsed commandline arguments")
        ("config,c", po::value<string>(), "config file name")
        ("file,f", po::value<string>(), "stdf file name")
        ("stdf-fields,s", po::value<string>(), "stdf field list, multiple values separated by ," )
        ("test-nos,t", po::value<string>(), "test numbers, multiple values separated by ," )
        ("debug,d", "set to debug mode. This prints the output to screen");

    try {
        po::store(po::parse_command_line(arg, argv, op), vm);
        po::notify(vm);

        cout << "config file got: " <<  vm["config"].as<string>() << endl;
        cout << "stdf file got: " << vm.count("file") <<  vm["file"].as<string>() << endl;
        return (vm.count("file") > 0);

    }
    catch(...) {
        cout << "problem here" << endl;
        cerr << "Exception of unknown type flag or line argument construct passed in !\n";
    }

    return false;
}


// destructor
cmdlineopts::~cmdlineopts() {

}

bool cmdlineopts::has_stdffields() const {
    return vm.count("stdf-fields");
}

bool cmdlineopts::has_stdffile() const {
    return vm.count("file");
}

bool cmdlineopts::has_testnumbers() const {
    return vm.count("test-nos");
}

bool cmdlineopts::has_configfile() const {
    return vm.count("config");
}


bool cmdlineopts::has_debug() const {
    return vm.count("debug");
}

string cmdlineopts::stdf_filename() const {
    return vm["file"].as<string>();
}


string cmdlineopts::config_filename() const {
    return vm["config"].as<string>();
}


vector<string> cmdlineopts::stdffields() const {
    // should call has_stdffields before proceeding

    string val = vm["stdf-fields"].as<string>();
    // check if there is multiple
    vector<string> f;  // store recs
    split( f, val, is_any_of(","), token_compress_on );

    if ((int)f.size()>0) {
        for (int i=0; i< (int)f.size(); i++) {
            trim(f[i]);  // trim space
        }
    }

    return f;

}


vector<int> cmdlineopts::testnumbers() const {
    // should call has_testnumbers before proceeding

    std::string val = vm["test-nos"].as<string>();
    // check if there is multiple
    vector<string> f;  // store recs
    split( f, val, is_any_of(","), token_compress_on );
    if ((int)f.size()>0) {
        for (int i=0; i< (int)f.size(); i++) {
            trim(f[i]);
        }
    }

    vector<int> fi;
    vector<string>::const_iterator it;
    for (it=f.begin(); it!=f.end(); it++) {
         int i = atoi( (*it).c_str());
         fi.push_back(i);
    }

    return fi;

}
